package com.ag.hmk.service.impl;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordToHtmlSerImpl {
	/**
	 * 
	 * @param inFileString  完整原文件路径（如：E:\\HMK\\my.doc）
	 * @param outFileString  完整的目标文件路径（如：F:\\TEST\\2.html）
	 * @return boolean 
	 */
	public boolean wordToHtml(String inFileString, String outFileString) {
		ActiveXComponent app = new ActiveXComponent("Word.Application");// 查找word组件
		boolean flag = false;
		try {
			app.setProperty("Visible", new Variant(false));// 设置word不可见
			Object docs = app.getProperty("Documents").toDispatch();

			Object doc = Dispatch.invoke(
					(Dispatch) docs,
					"Open",
					Dispatch.Method,
					new Object[] { inFileString, new Variant(false),
							new Variant(true) }, new int[1]).toDispatch();// 打开word文件，注意这里第三个参数要设为true，这个参数表示是否以只读方式打开，因为我们不用保存原文件，所以以只读方式打开，如果你想进行读写，那么就得设为false。

			Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
					new Object[] { outFileString, new Variant(8) }, new int[1]);// 作为html格式保存到目标文件

			Variant f = new Variant(false);
			Dispatch.call((Dispatch) doc, "Close", f); // 关闭word文件
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("-------error!");
		} finally {
			System.out.println("---vvvvvvvvvvfsssssssssss---");
			app.invoke("Quit", new Variant[] {});

		}
		return flag;
	}
	/**
	 * 
	 * @param srcFileString
	 * @param objFileString
	 * @return 文件复制服务
	 */
	public boolean copyFile(String srcFileString , String objFileString){
		boolean flag = false;
		try {
			File srcFile = new File(srcFileString);
			File objFile = new File(objFileString);
			
			FileUtils.copyFile(srcFile, objFile);
			flag = true ;
			System.out.println("===copy file ok");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("===copy file error");
		}
		
		return flag;
	}
	
	
	/**
	 * 
	 * @param srcFileString
	 * @param objFileString
	 * @return 文件夹复制服务
	 */
	public boolean copyFoler(String srcFoler , String objFoler){
		boolean flag = false;
		try {
			File srcFile = new File(srcFoler);
			File objFile = new File(objFoler);
			
			FileUtils.copyDirectory(srcFile, objFile);
			//FileUtils.copyFile(srcFile, objFile);
			flag = true ;
			System.out.println("===copy foler ok");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("===copy foler error");
		}
		
		return flag;
	}
	
	/**
	 * 
	 * @param srcFileString
	 * @param objFileString
	 * @return 文件是否存在服务
	 */
	public boolean isFileExist(String filePath){
		boolean flag = false;
		try {
			//File srcFile = new File(srcFoler);
			File myFile = new File(filePath);
			if (myFile.exists()) {
				flag = true ;
			} 
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("===copy foler error");
		}
		
		return flag;
	}
	
}
